package org.googlecode.viewt.core.tools;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xjavadoc.SourceClass;

public class LogicDescRepository {
	private static final Logger logger = LoggerFactory
			.getLogger(LogicDescRepository.class);

	private Map classMap = new HashMap();

	private String blintClassName = null;

	private String blimpClassName = null;

	public void setBLFile(String blintFile, String blimpFile) {
		if ((blintFile == null) || (blimpFile == null)) {
			logger
					.error("Interface source or implement source of business logic not set, check your config");
			return;
		}

		SourceClass intClass = JavaSourceParser.parseFile(blintFile)[0];
		if ((intClass != null)
				&& (this.classMap.containsKey(intClass.getQualifiedName())))
			intClass = (SourceClass) this.classMap.get(intClass
					.getQualifiedName());
		SourceClass impClass = JavaSourceParser.parseFile(blimpFile)[0];
		if ((impClass != null)
				&& (this.classMap.containsKey(impClass.getQualifiedName())))
			impClass = (SourceClass) this.classMap.get(impClass
					.getQualifiedName());
		if (!(JavaSourceParser.validateBLClass(intClass, impClass))) {
			logger.error("Invalid business logic source");
			return;
		}

		this.blintClassName = intClass.getQualifiedName();
		this.blimpClassName = impClass.getQualifiedName();
		this.classMap.put(this.blintClassName, intClass);
		this.classMap.put(this.blimpClassName, impClass);

		if (logger.isDebugEnabled())
			logger
					.debug("Parse interface and implement source succeed, intFile:"
							+ blintFile + ", impFile:" + blimpFile);
	}

	public void addBLClass(SourceClass blClass) {
		if (blClass == null) {
			return;
		}
		String className = blClass.getQualifiedName();
		if (this.classMap.containsKey(className))
			return;
		this.classMap.put(className, blClass);
		if (logger.isDebugEnabled())
			logger.debug("class:" + className + " not found in map, added");
	}

	public void addBLClass(String fileOrPathName) {
		if (fileOrPathName == null) {
			return;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("adding " + fileOrPathName
					+ " to java source repository for analysis");
		}
		SourceClass[] blClasses = JavaSourceParser.parseFile(fileOrPathName);
		int size = (blClasses == null) ? 0 : blClasses.length;
		for (int i = 0; i < size; ++i) {
			addBLClass(blClasses[i]);

			if (logger.isDebugEnabled())
				logger.debug("class:" + blClasses[i].getQualifiedName()
						+ " add to source repository");
		}
	}

	public SourceClass getBLClass(String className) {
		if (className != null) {
			return ((SourceClass) this.classMap.get(className));
		}
		return null;
	}

	public SourceClass getBLInterface() {
		if (this.blintClassName == null) {
			return null;
		}
		return getBLClass(this.blintClassName);
	}

	public String getBLInterfaceName() {
		return this.blintClassName;
	}

	public SourceClass getBLImplement() {
		if (this.blimpClassName == null) {
			return null;
		}
		return getBLClass(this.blimpClassName);
	}

	public String getBLImplementName() {
		return this.blimpClassName;
	}

	public Map getClassMap() {
		return this.classMap;
	}
}